const EventsContainer = {
  pools:{

  } as {[key:string]:{pools:any}&EventClass},
  func:{
    $emit(this:{pools:any},evt:string,params?:{[key:string]:any}){
      if(this.pools[evt]==null)return;
      this.pools[evt].forEach((m:Function)=>{
        m(params);
      })
    },
    $off(this:{pools:any},evt:string,callback:Function){
      if(this.pools[evt]==null)return;
      const idx = this.pools[evt].findIndex((m:Function)=>m==callback);
      if(idx>-1){
        this.pools[evt].splice(idx,1);
      }
    },
    $on(this:{pools:any},evt:string,callback:(params?:{[key:string]:any})=>void){
      this.pools[evt] = this.pools[evt]||[];
      this.pools[evt].push(callback);
    }
  } as EventClass
};

export const EventHandle = {
  register(id:string):EventClass{
    if(EventsContainer.pools[id]!=null){
      return EventsContainer.pools[id];
    }
    EventsContainer.pools[id] = {
      pools:{},
      ...EventsContainer.func
    }
    return EventsContainer.pools[id];
  },
  unregister(id:string){
    if(EventsContainer.pools[id]!=null){
      delete EventsContainer.pools[id];
    }
  }
}